<template>
  <transition name="mini">
    <div class="mini-player" v-if="!fullScreen" @click="showPlayer">
      <div class="icon">
        <img
          :class="pauseRotate"
          width="40"
          height="40"
          :src="currentSong.image || defaultImg"
        />
      </div>
      <div class="text">
        <h2 class="name">{{currentSong.name}}</h2>
        <p class="desc">{{currentSong.singer}}</p>
      </div>
      <div class="control" @click.stop="togglePlaying">
        <i class="iconfont icon-play-status" v-html="playIcon"></i>
      </div>
      <div class="control" @click.stop="handlePlayList">
        <i class="iconfont icon-playlist">&#xe674;</i>
      </div>
    </div>
  </transition>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import defaultImg from 'assets/images/default_music.png'

export default {
  name: 'MiniPlayer',
  computed: {
    // 图片旋转状态
    pauseRotate () {
      return this.playStatus ? 'play' : 'play pause'
    },
    playIcon () { // 播放暂停图标
      return this.playStatus ? '&#xe669;' : '&#xe666;'
    },
    ...mapGetters([
      'playStatus',
      'fullScreen',
      'showPlayList',
      'currentSong'
    ])
  },
  data () {
    return {
      defaultImg
    }
  },
  methods: {
    showPlayer () { // 显示全屏播放器
      if (Object.keys(this.currentSong).length === 0) {
        return
      }
      this.setFullScreen(true)
    },
    handlePlayList () { // 显示播放列表
      if (Object.keys(this.currentSong).length === 0) {
        return
      }
      this.setPlayList(true)
    },
    togglePlaying () { // 切换播放、暂停
      if (Object.keys(this.currentSong).length === 0) {
        return
      }
      this.setPlayingState(!this.playStatus)
    },
    // 切换状态
    ...mapMutations({
      setPlayingState: 'SET_PLAYING_STATE',
      setPlayList: 'SET_SHOW_PLAYLIST',
      setFullScreen: 'SET_FULL_SCREEN'
    })
  }
}
</script>

<style lang="less" scoped>
@import '~assets/style/variable';
@import '~assets/style/mixin';
.mini-player {
  display: flex;
  align-items: center;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 100;
  width: 100%;
  height: 50px;
  border-top: 1px solid @border-color;
  background-color: var(--bg-color);
  .icon {
    flex: 0 0 40px;
    width: 40px;
    padding: 0 10px 0 20px;
    img {
      border-radius: 50%;
      &.play {
        animation: rotate 10s linear infinite;
      }
      &.pause {
        animation-play-state: paused;
      }
    }
  }
  .text {
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
    line-height: 20px;
    overflow: hidden;
    .name {
      .no-wrap();
      font-size: @fs-medium;
      color: var(--text-color);
    }
    .desc {
      .no-wrap();
      font-size: @fs-small;
      color: var(--text-color);
    }
  }
  .control {
    flex: 0 0 30px;
    width: 30px;
    padding: 0 5px;
    .icon-play-status,
    .icon-playlist {
      font-size: 30px;
      color: var(--theme-color);
    }
  }
}
@keyframes rotate {
  0% {
    transform: rotate(0deg)
  }
  100% {
    transform: rotate(360deg)
  }
}
</style>
